package com.example.chatweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.chatweb.entity.Opinion;
import com.example.chatweb.mapper.HistoryMapper;
import com.example.chatweb.mapper.OpinionMapper;
import com.example.chatweb.service.OpinionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OpinionServiceImpl implements OpinionService {
    @Autowired
    private OpinionMapper opinionMapper;

    @Override
    public void addOpinion(Opinion opinion){opinionMapper.insert(opinion);}

    @Override
    public void addOrUpdateOpinion(Opinion opinion) {
        QueryWrapper<Opinion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("file_id", opinion.getFileId());
        Opinion existingOpinion = opinionMapper.selectOne(queryWrapper);

        if (existingOpinion != null) {
            // 如果存在则更新记录
            opinionMapper.updateById(opinion);
        } else {
            // 如果不存在则插入新记录
            opinionMapper.insert(opinion);
        }
    }
    @Override
    public void update(Opinion opinion){opinionMapper.updateById(opinion);}

    @Override
    public String getContentByID(Integer fileId){
        Opinion opinion = opinionMapper.selectById(fileId);
        if(opinion != null) {
            return opinion.getContent();
        }
        else {
            return null;
        }
    }


//    public List<Integer> getId{opinionMapper.}
}
